package base.file.api;


import java.nio.charset.Charset;

// 文件编码 解码
public class ContentEncodeDecode {
    public static void main(String[] args) {
        String data = "Ghidra软件逆向工程框架";

        // 工程默认字符集
        byte[] bytesSysDefault = data.getBytes();
        System.out.println(bytesSysDefault.length);

        String decodeDataSysDefault = new String(bytesSysDefault);

        System.out.println(decodeDataSysDefault);

        // 指定字符集
        byte[] appointCharset = data.getBytes(Charset.forName("UTF-16BE"));

        // 出现乱码原因
        // 1. 解码字符集错误
        String errorCharset = new String(appointCharset, Charset.forName("UTF-8"));
        System.out.println(errorCharset);

        // 2. 字符长度错误
        String errorCharLength = new String(appointCharset,  1 , appointCharset.length - 3,  Charset.forName("UTF-16BE"));
        System.out.println(errorCharLength);

        System.out.println("正确: "+  new String(appointCharset, Charset.forName("UTF-16BE")));
    }

}
